<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
    -->
    <b-modal ref="my-modal" size="lg" :title="title" scrollable no-close-on-backdrop>
        <div class="d-block text-left">
            <b-card
                header="基础信息"
                bg-variant="light"
                title=""
            >
                <template v-slot:header>
                    <h6 class="mb-0">基础信息</h6>
                </template>
                <b-card-text>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">ip</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="64" v-model="deviceConfigForm.ip"
                                placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">用&nbsp;户&nbsp;&nbsp;名</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="32" v-model="deviceConfigForm.userName"
                                placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                        <div class="col-md-3">
                            <input class="form-control" type="text" maxlength="32" v-model="deviceConfigForm.password"
                                placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">端&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;口</label>
                        <div class="col-md-1">
                            <el-input-number v-model="deviceConfigForm.port" maxlength="6" style="width:100px;"
                                controls-position="right" :min="0" :max="999999">
                            </el-input-number>
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">通信协议</label>
                        <div class="col-md-2">
                            <el-select maxlength="20" v-model="deviceConfigForm.protocol"
                                placeholder="请输入">
                                <el-option
                                    v-for="item in [{value:'',label:'请选择'},{value:'TCP',label:'TCP'},
                                    {value:'UDP',label:'UDP'},{value:'RTU',label:'RTU'},{value:'ASCII',label:'ASCII'},
                                    {value:'HTTP',label:'HTTP'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    
                </b-card-text>
            </b-card>

            <b-card  v-if="showXJ == true"
                header="摄像机参数"
                bg-variant="light"
            >
                <template v-slot:header>
                    <h6 class="mb-0">摄像机参数</h6>
                </template>
                <b-card-text>
                    <div class="form-group m-form__group row" v-if="showXJ == true">
                        <label class="col-md-2 col-form-label">厂商类型</label>
                        <div class="col-md-2">
                            <el-select v-model="deviceConfigForm.cameraTypeId" placeholder="请选择">
                                <el-option
                                v-for="item in cameraTypeList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </div>
                        <label class="col-md-2 col-form-label">码&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;流</label>
                        <div class="col-md-2">
                            <el-select maxlength="20" v-model="deviceConfigForm.subtype"
                                placeholder="请输入">
                                <el-option
                                    v-for="item in [{value:'',label:'请选择'},{value:0,label:'主码流'},
                                    {value:1,label:'子码流'},{value:2,label:'辅码流'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="form-group m-form__group row"  v-if="showXJ == true">
                        <label class="col-md-2 col-form-label">rtsp通道号</label>
                        <div class="col-md-2">
                            <el-input-number v-model="deviceConfigForm.channelNo" maxlength="6" style="width:100px;"
                                controls-position="right" :min="0" :max="999999">
                            </el-input-number>
                        </div>
                        <label class="col-md-2 col-form-label">rtsp端口</label>
                        <div class="col-md-2">
                            <el-input-number v-model="deviceConfigForm.rtspPsort" maxlength="6" style="width:100px;"
                                controls-position="right" :min="0" :max="999999">
                            </el-input-number>
                        </div>
                        <label class="col-md-2 col-form-label" title="存储服务器通道号">nvr通道号</label>
                        <div class="col-md-2">
                            <el-input-number v-model="deviceConfigForm.nvrChannel" maxlength="6" style="width:100px;"
                                controls-position="right" :min="0" :max="999999">
                            </el-input-number>
                        </div>                        
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">自定义rtsp</label>
                        <div class="col-md-10">
                            <input class="form-control" type="text" maxlength="255" v-model="deviceConfigForm.rtspFull"
                                placeholder="请输入">
                        </div>
                    </div>
                    <div class="form-group m-form__group row">
                        <label class="col-md-2 col-form-label">采用自定义rtsp</label>
                        <div class="col-md-2">
                            <el-select maxlength="20" v-model="deviceConfigForm.useRtspFull"
                                placeholder="请输入">
                                <el-option
                                    v-for="item in [{value:0,label:'否'},{value:1,label:'是'}]"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </b-card-text>
            </b-card>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
            <button type="button" class="btn btn-light-danger font-weight-bold mr-2" block @click="hideModal">
                <i class="icon-xl fa fa-times"></i>取消
            </button>
            <button type="button" class="btn btn-light-success font-weight-bold mr-2" @click="updateDeviceConfig">
                <i class="icon-xl fas fa-save"></i> 保存
            </button>
        </template>
    </b-modal>
</template>
<script>
import apiUtil from "@/core/util/apiUtil.js";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {
            title: "设备配置",
            showXJ:true,
            cameraTypeList:[],
            deviceConfigForm: {
                deviceId: "",
                id: "",
                ip: "",
                port: "",
                userName: "",
                password: "",
                protocol: "",
                type: null,
                subtype: null,
                channelNo: "",
                rtspPort: "",
                nvrChannel: "",
                useRtspFull:0,
                rtspFull:""
            }
        }
    },
    components: {

    },
    watch: {
    },
    mounted() {
        this.initCameraTypeList();
    },
    methods: {
        updateDeviceConfig() {
            if (this.deviceConfigForm.deviceId === "" || this.deviceConfigForm.deviceId === null) {
                handleAlert("未能获取到设备id", "warning", 3)
                return;
            }
            if (this.deviceConfigForm.ip === ""  || this.deviceConfigForm.ip === null) {
                handleAlert("请输入ip", "warning", 3)
                return;
            }
            // 保存前提示
            this.$confirm("确定保存?", "提示", { type: "warning", }).then(() => {
                apiUtil.update(process.env.VUE_APP_MONITOR_API + "/iotDeviceConfig/update", this.deviceConfigForm).then(({ data }) => {
                    if (data.success) {
                        handleAlert("操作成功", "success", 3);
                        this.hideModal();//关闭窗体
                        this.$emit("change", data);//组件传值，即向父级模态框中传递值，采用change方式，目标刷新主列表使用
                    } else {
                        handleAlert("操作失败", "error", 3);
                    }
                });
            }).catch(() => { });//注意这里，这里是重点！！！;   
        },
        showModal(row) {
            this.restForm();
            this.title = "设备配置-关联设备【" + row.name + "】";
            this.$refs['my-modal'].show();
            this.deviceConfigForm.deviceId = row.id;
            if(row.deviceTypeId){
                apiUtil.get(process.env.VUE_APP_MONITOR_API + "/iotDeviceType/get/" +row.deviceTypeId).then(({ data }) => {
                    if(data.data.tag == 'QCAM' || data.data.tag == 'YCAM' || data.data.tag == 'GCAM'){
                        this.showXJ = true;                    
                    }else{
                        this.showXJ = false;   
                    }
                });
            }
            this.getDetail(row.id);
        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        restForm() {
            //重置
            Object.assign(this.$data.deviceConfigForm, this.$options.data().deviceConfigForm);
        },
        getDetail(id) {
            apiUtil.get(process.env.VUE_APP_MONITOR_API + "/iotDeviceConfig/get/" + id).then(({ data }) => {
                if (null == data.data || "" == data.data) {

                } else {
                    this.deviceConfigForm = data.data;
                    this.deviceConfigForm.deviceId = id;
                }
            });
        },
        initCameraTypeList(){
            let params = {};
            apiUtil.query(process.env.VUE_APP_MONITOR_API+"/iotCameraType/allList", params).then(({ data }) => {
                this.cameraTypeList = data.data;
            });
        },
    }
}
</script>